<?php
/**
 * Jishupu-Laravel-Vue-Admin
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2021/11/17
 */

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Pagination\AbstractPaginator;

/**
 * 自定义基础集合类
 * Class BaseCollection
 * @package App\Http\Resources
 */
class BaseCollection extends ResourceCollection
{

    /**
     * 模式:onlg仅仅展示、except排除、default原始返回，默认：default
     * @var string
     */
    private $model = 'default';

    /**
     * 需过滤的普通字段：针对无嵌套或一对一关联模型关联
     * @var array
     */
    private $normalFields = [];

    /**
     * 需过滤的数组字段：针对一对多模型关联
     * @var array
     */
    private $arrayFields = [];

    /**
     * 拆分需过滤的普通字段、数组字段
     * @param $fields
     */
    function parseFilterFields($fields)
    {
        collect($fields)->map(function ($item, $key) {
            if (is_array($item)) {
                $this->arrayFields[$key] = $item;
                if ($this->model == "only") {
                    $this->normalFields[] = $key;
                }
            } else {
                $this->normalFields[] = $item;
            }
        });
    }

    /**
     * only 仅展示，例：['id', 'title', 'items.name', dictionaryItems => ['id', 'name']]
     * 说明：items为一对一模型关联数据，dictionaryItems为一对多模型关联数据
     * @param array $fields
     * @return $this
     */
    public function only(array $fields)
    {
        $this->model = 'only';
        $this->parseFilterFields($fields);
        return $this;
    }

    /**
     * except 排除，例：['id', 'title', 'items.name', dictionaryItems => ['id', 'name']]
     * 说明：items为一对一模型关联数据，dictionaryItems为一对多模型关联数据
     * @param array $fields
     * @return $this
     */
    public function except(array $fields)
    {
        $this->model = 'except';
        $this->parseFilterFields($fields);
        return $this;
    }

    /**
     * 重写集合转数组
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        $collection = $this->collection->map(function ($item) {
            if ($this->model == "only") {
                return collect($item)->only($this->normalFields)->map(function ($item, $key) {
                    $only_key = $key;
                    if (is_object($item) && count($this->arrayFields) > 0) {
                        return $item->map(function ($item, $key) use ($only_key) {
                            return collect($item)->only($this->arrayFields[$only_key])->all();
                        });
                    } else {
                        return $item;
                    }
                })->all();
            } else if ($this->model == "except") {
                return collect($item)->except($this->normalFields)->map(function ($item, $key) {
                    $except_key = $key;
                    if (is_object($item) && count($this->arrayFields) > 0) {
                        return $item->map(function ($item, $key) use ($except_key) {
                            return collect($item)->except($this->arrayFields[$except_key])->all();
                        });
                    } else {
                        return $item;
                    }
                })->all();
            } else {
                return $item;
            }
        });
        // 分页情况
        if ($this->resource instanceof AbstractPaginator) {
            return [
                'items' => $collection,
                'meta' => [
                    'current_page' => $this->resource->currentPage(),
                    'last_page' => $this->resource->lastPage(),
                    'per_page' => $this->resource->perPage(),
                    'total' => $this->resource->total()
                ]
            ];
        } else {
            return [
                'items' => $collection,
            ];
        }
    }


}
